/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package give.replay.gui;

import give.replay.config.ReplayConfiguration;
import give.matchmaker.MatchmakerConfiguration;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author koller
 */
public abstract class AbstractMain {
    protected MatchmakerConfiguration config;
    protected DbSelectionInterface dbSelectionInterface;
    protected ReplayConfiguration replayConfig;

    public AbstractMain(String configFilename) {
        config = new MatchmakerConfiguration(null);
        config.readFromFile(configFilename);

        replayConfig = new ReplayConfiguration();
        replayConfig.readFromFile(configFilename);

        dbSelectionInterface = null;
    }

    public void openDbSelectionInterface() {
        if (dbSelectionInterface == null) {
            List<Map<String,Object>> entries = new ArrayList<Map<String, Object>>();
            try {
                java.sql.Statement stmt = config.getDb().getDatabaseConnection().createStatement();
                ResultSet rs = stmt.executeQuery("select * from " + replayConfig.getDbTableName());

                while (rs.next()) {
                    entries.add(replayConfig.convertResultSet(rs));
                }

                dbSelectionInterface = new DbSelectionInterface(this, entries, replayConfig);
            } catch (SQLException ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }

        dbSelectionInterface.setVisible(true);
    }

    public abstract void jumpTo(int eventIndex);

    public abstract void openMap(int gameId);
}
